<?php declare(strict_types=1);

namespace App\Bac\Controller\RBAC;

use App\Helper\RespHelper;
use App\Abstract\Controller;
use App\Bac\Model\RBAC\Admin;
use Hyperf\Di\Annotation\Inject;
use App\Bac\Handler\RBAC\MenuHandler;
use App\Bac\Request\RBAC\MenuRequest;

class MenuController extends Controller
{
    #[Inject]
    protected MenuHandler $handler;

    public function index(): array
    {
        return RespHelper::success($this->handler->getList());
    }

    public function create(): array
    {
        $request = make(MenuRequest::class);
        $request->scene('add')->validateResolved();
        $data = $request->all();
        if (!$this->handler->addMenu($data))
            return RespHelper::fail();
        return RespHelper::success();
    }

    public function edit(): array
    {
        $request = make(MenuRequest::class);
        $request->scene('edit')->validateResolved();
        $data = $request->all();
        $id = $data['id'];
        unset($data['id']);
        if (!$this->handler->editMenu((int)$id, $data))
            return RespHelper::fail();
        return RespHelper::success();
    }

    public function del(): array
    {
        $ids = $this->request->input('ids');
        if (!$this->handler->delMenu($ids))
            return RespHelper::fail();
        return RespHelper::success();
    }

    /**
     * 用户菜单
     * @return array
     */
    public function getMenuByAdmin(): array
    {
        $curAdmin = Admin::getCurAdmin();
        $menuList = $this->handler->getMenuByAdmin($curAdmin->id, $curAdmin->isSuper());
        return RespHelper::success($menuList);
    }
}
